home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Library / Manuels & Misc / Assembly / AOA.ZIP / CH19 / COCALL32.ASM < prev    next >
Encoding:
Assembly Source File  |  1994-07-20  |  4.4 KB  |  199 lines

  1.  
  2.         .386
  3.         option    segment:use16
  4.  
  5. dseg        segment    para public 'data'
  6.  
  7. wp        equ    <word ptr>
  8.  
  9. ; 32-bit PCB.  Note we only keep the L.O. 16 bits of SP since we are
  10. ; operating in real mode.
  11.  
  12. pcb32        struc
  13. regsp        word    ?
  14. regss        word    ?
  15. regip        word    ?
  16. regcs        word    ?
  17.  
  18. regeax        dword    ?
  19. regebx        dword    ?
  20. regecx        dword    ?
  21. regedx        dword    ?
  22. regesi        dword    ?
  23. regedi        dword    ?
  24. regebp        dword    ?
  25.  
  26. regds        word    ?
  27. reges        word    ?
  28. regflags    dword    ?
  29. pcb32        ends
  30.  
  31.  
  32. DefaultPCB    pcb32    <>
  33. DefaultCortn    pcb32    <>
  34.  
  35. CurCoroutine    dd    DefaultCortn    ;Points at the currently executing
  36.                     ; coroutine.
  37. dseg        ends
  38.  
  39.  
  40. cseg        segment    para public 'slcode'
  41.  
  42. ;============================================================================
  43. ;
  44. ; Coroutine support.
  45. ;
  46. ; COINIT- ES:DI contains the address of the current (default) process.
  47.  
  48. CoInit32    proc    far
  49.         assume    ds:dseg
  50.         push    ax
  51.         push    ds
  52.         mov    ax, dseg
  53.         mov    ds, ax
  54.         mov    wp dseg:CurCoroutine, di
  55.         mov    wp dseg:CurCoroutine+2, es
  56.         pop    ds
  57.         pop    ax
  58.         ret
  59. CoInit32    endp
  60.  
  61.  
  62. ; COCALL32- transfers control to a coroutine.  ES:DI contains the address
  63. ; of the PCB.  This routine transfers control to that coroutine and then
  64. ; returns a pointer to the caller's PCB in ES:DI.
  65.  
  66. cocall32    proc    far
  67.         assume    ds:dseg
  68.         pushfd
  69.         push    ds
  70.         push    es            ;Save these for later
  71.         push    edi
  72.         push    eax
  73.         mov    ax, dseg
  74.         mov    ds, ax
  75.         cli                ;Critical region ahead.
  76.  
  77.  
  78. ; Save the current process' state:
  79.  
  80.         les    di, dseg:CurCoroutine
  81.         pop    es:[di].pcb32.regeax
  82.         mov    es:[di].pcb32.regebx, ebx
  83.         mov    es:[di].pcb32.regecx, ecx
  84.         mov    es:[di].pcb32.regedx, edx
  85.         mov    es:[di].pcb32.regesi, esi
  86.         pop    es:[di].pcb32.regedi
  87.         mov    es:[di].pcb32.regebp, ebp
  88.  
  89.         pop    es:[di].pcb32.reges
  90.         pop    es:[di].pcb32.regds
  91.         pop    es:[di].pcb32.regflags
  92.         pop    es:[di].pcb32.regip
  93.         pop    es:[di].pcb32.regcs
  94.         mov    es:[di].pcb32.regsp, sp
  95.         mov    es:[di].pcb32.regss, ss
  96.  
  97.         mov    bx, es            ;Save so we can return in
  98.         mov    ecx, edi        ; ES:DI later.
  99.         mov    edx, es:[di].pcb32.regedi
  100.         mov    es, es:[di].pcb32.reges
  101.         mov    di, dx            ;Point es:di at new PCB
  102.  
  103.         mov    wp dseg:CurCoroutine, di
  104.         mov    wp dseg:CurCoroutine+2, es
  105.  
  106.         mov    es:[di].pcb32.regedi, ecx ;The ES:DI return values.
  107.         mov    es:[di].pcb32.reges, bx
  108.  
  109. ; Okay, switch to the new process:
  110.  
  111.         mov    ss, es:[di].pcb32.regss
  112.         mov    sp, es:[di].pcb32.regsp
  113.         mov    eax, es:[di].pcb32.regeax
  114.         mov    ebx, es:[di].pcb32.regebx
  115.         mov    ecx, es:[di].pcb32.regecx
  116.         mov    edx, es:[di].pcb32.regedx
  117.         mov    esi, es:[di].pcb32.regesi
  118.         mov    ebp, es:[di].pcb32.regebp
  119.         mov    ds, es:[di].pcb32.regds
  120.  
  121.         push    es:[di].pcb32.regflags
  122.         push    es:[di].pcb32.regcs
  123.         push    es:[di].pcb32.regip
  124.         push    es:[di].pcb32.regedi
  125.         mov    es, es:[di].pcb32.reges
  126.         pop    edi
  127.         iret
  128. cocall32    endp
  129.  
  130.  
  131. ; CoCall32l works just like cocall above, except the address of the pcb
  132. ; follows the call in the code stream rather than being passed in ES:DI.
  133. ; Note: this code does *not* return the caller's PCB address in ES:DI.
  134. ;
  135.  
  136. cocall32l    proc    far
  137.         assume    ds:dseg
  138.         push    ebp
  139.         mov    bp, sp
  140.         pushfd
  141.         push    ds
  142.         push    es
  143.         push    edi
  144.         push    eax
  145.         mov    ax, dseg
  146.         mov    ds, ax
  147.         cli                ;Critical region ahead.
  148.  
  149. ; Save the current process' state:
  150.  
  151.         les    di, dseg:CurCoroutine
  152.         pop    es:[di].pcb32.regeax
  153.         mov    es:[di].pcb32.regebx, ebx
  154.         mov    es:[di].pcb32.regecx, ecx
  155.         mov    es:[di].pcb32.regedx, edx
  156.         mov    es:[di].pcb32.regesi, esi
  157.         pop    es:[di].pcb32.regedi
  158.         pop    es:[di].pcb32.reges
  159.         pop    es:[di].pcb32.regds
  160.         pop    es:[di].pcb32.regflags
  161.         pop    es:[di].pcb32.regebp
  162.         pop    es:[di].pcb32.regip
  163.         pop    es:[di].pcb32.regcs
  164.         mov    es:[di].pcb32.regsp, sp
  165.         mov    es:[di].pcb32.regss, ss
  166.  
  167.         mov    dx, es:[di].pcb32.regip    ;Get return address (ptr to
  168.         mov    cx, es:[di].pcb32.regcs    ; PCB address.
  169.         add    es:[di].pcb32.regip, 4    ;Skip ptr on return.
  170.         mov    es, cx            ;Get the ptr to the new pcb
  171.         mov    di, dx            ; address, then fetch the
  172.         les    di, es:[di]        ; pcb val.
  173.         mov    wp dseg:CurCoroutine, di
  174.         mov    wp dseg:CurCoroutine+2, es
  175.  
  176. ; Okay, switch to the new process:
  177.  
  178.         mov    ss, es:[di].pcb32.regss
  179.         mov    sp, es:[di].pcb32.regsp
  180.         mov    eax, es:[di].pcb32.regeax
  181.         mov    ebx, es:[di].pcb32.regebx
  182.         mov    ecx, es:[di].pcb32.regecx
  183.         mov    edx, es:[di].pcb32.regedx
  184.         mov    esi, es:[di].pcb32.regesi
  185.         mov    ebp, es:[di].pcb32.regebp
  186.         mov    ds, es:[di].pcb32.regds
  187.  
  188.         push    es:[di].pcb32.regflags
  189.         push    es:[di].pcb32.regcs
  190.         push    es:[di].pcb32.regip
  191.         push    es:[di].pcb32.regedi
  192.         mov    es, es:[di].pcb32.reges
  193.         pop    edi
  194.         iret
  195.  
  196.  
  197. cocall32l    endp
  198. cseg        ends
  199.         end